package controllers.admin;

import play.*;
import play.mvc.*;
import service.TransPriceService;

import java.util.*;

import controllers.Secure;

import models.*;
import models.BaseEntity.Status;
import models.Message.MessageStatus;
import models.Message.MessageType;

@With(Secure.class)
public class Areas extends Controller {

	/**
	 * 进入省份管理
	 */
	public static void indexProvinces(){
		
		List<Province> provList = Province.findAll();
		
		render(provList);
	}
	
	/**
	 * 删除省份
	 */
	public static void deleteProvince(long id){
		//查找省份
		Province pro = Province.findById(id);
		
		if(pro.cityList != null){
			for(City city : pro.cityList){
				deleteCity(id,city.id);
			}
		}
		
		//删除省份
		pro.delete();
		
		render("@indexProvinces");
	}
	
	/**
	 * 创建省份
	 */
	public static void createProvince(String codeVal,String nameVal){
		
		Province pro = new Province();
		pro.code = codeVal;
		pro.name = nameVal;
		pro.save();
		
		render("@indexProvinces");
	}
	
	/**
	 * 修改省份
	 */
	public static void updateProvince(String codeVal,String nameVal,long proId){
		Province pro = Province.findById(proId);
		pro.code = codeVal;
		pro.name = nameVal;
		pro.save();
		
		render("@indexProvinces");
	}
	
	/**
	 * 进入城市管理页面
	 */
	public static void indexCity(QueryBean queryBean,long provid){
		if(queryBean == null) {
			queryBean = new QueryBean();
		}
		
		Province pro = Province.findById(provid);
		List<City> cityList = pro.cityList;
		
		render(queryBean,cityList,provid);
	}
	
	/**
	 * 修改城市
	 */
	public static void updateCity(String codeVal,String nameVal,long cityId){
		City city = City.findById(cityId);
		city.cityCode = codeVal;
		city.cityName = nameVal;
		city.save();
		
		render("@indexCity");
	}
	
	/**
	 * 创建城市
	 */
	public static void createCity(String codeVal,String nameVal,long provid){
		
		//查询出省份
		Province pro = Province.findById(provid);
		
		List<City> cityList = pro.cityList;
		if(cityList==null || cityList.size()==0){
			cityList = new ArrayList<City>();
		}
		
		//创建城市
		City city = new City();
		city.cityCode = codeVal;
		city.cityName = nameVal;
		city.province = pro;
		city.save();
		
		//保存省份修改
		cityList.add(city);
		pro.save();
		
		render("@indexCity");
	}
	
	/**
	 * 删除城市
	 */
	public static void deleteCity(long provid,long cityId){
		//查找省份
		Province pro = Province.findById(provid);
		
		//删除省份与城市的关联关系
		List<City> cityList = pro.cityList;
		
		Iterator it = cityList.iterator();
		while(it.hasNext()){
			City temp = (City)it.next();
			if(temp.id == cityId){
				it.remove();
			}
		}
		
		pro.save();
		
		//删除运价与城市的关联
		List<TransferPrice> priceList = TransferPrice.find("startCity.id=? or endCity.id=?", cityId,cityId).fetch();
		if(priceList!=null && priceList.size()!=0){
			//删除运价与公司的关联
			for(TransferPrice price : priceList){
				if(price.startCity.id==cityId || price.endCity.id==cityId){
					price.startCity = null;
					price.endCity = null;
					price.company = null;
					
					//删除运价
					price.delete();
				}
			}
		}
		
		City city = City.findById(cityId);
		city.province = null;
		city.delete();
					
		render("@indexCity");
	}
    
}

